#ifndef UTILS_H
#define UTILS_H
#include <QDebug>
#include <QPoint>

class Utils{
public:
    static float getRadius(float x, float y, QPointF center)
    {
        float dx = x - center.x();
        float dy = y - center.y();
        float xx = dx*dx, yy = dy*dy;
        return sqrt(xx + yy);
    }

    //    float lerp(float a, float b, float t){
    //        return (1 - t) * a + t * b;
    //    }

    //    QPointF lerp(const QPointF& a, const QPointF& b, float t){
    //        return QPointF(lerp(a.x(), b.x(), t), lerp(a.y(), b.y(), t));
    //    }

    //    QPointF getLerp(const QPointF& a, const QPointF& b, const QPointF& c, float t){
    //         return QPointF(lerp(lerp(a, b, t), lerp(b, c, t), t));
    //    }

    static QPointF getLerp(const QPointF& a, const QPointF& b, const QPointF& c, float t){
        float u = 1 - t;
        return QPointF(
            u*u*a.x() + 2*u*t*b.x() + t*t*c.x(),
            u*u*a.y() + 2*u*t*b.y() + t*t*c.y()
        );
    }

    static QPointF getLerp(const QPointF& a, const QPointF& b, const QPointF& c, const QPointF& d, float t){
        float u = 1 - t;
        float tt = t * t;
        float uu = u * u;
        float uuu = uu * u;
        float utt = u * tt;
        float tuu = t * uu;
        float ttt = tt * t;

        return QPointF(
            uuu * a.x() + 3 * tuu * b.x() + 3 * utt * c.x() + ttt * d.x(),
            uuu * a.y() + 3 * tuu * b.y() + 3 * utt * c.y() + ttt * d.y()
        );
    }

    static QPointF normalize(const QPointF& dir)
    {
        float x = dir.x(), y = dir.y();
        float len = std::sqrt(x * x + y * y);
        if (len <= 0.000001) return dir;
        x /= len;
        y /= len;
        return QPointF(x, y);
    }

    static float PI(){
        static float PI = acos(-1);
        return PI;
    }
};

#endif // UTILS_H
